<?php
namespace app\api\controller;
use think\Controller;
use qr\QRcode as Qrcodes;
use file\FileUtil;
use app\api\model\QrCode as QrCodeModel;
use think\Db;
use app\api\model\Venueappoint;
use app\api\model\Venue;
/**
 * 
 * @author sj
 * 开始健身二维码生成控制器
 */
class Qrcode extends Controller{
    private static $Qrcode;
    private static  $QrCodeModel;
    protected $beforeActionList = [
        'first'
    ];
    public function first(){
        if(!self::$Qrcode){
            self::$Qrcode=new Qrcodes();
        }
        if(!self::$QrCodeModel){
            self::$QrCodeModel=new QrCodeModel();
        }
        
    }
    public function createQrCode(){
        $appkey=config('QrKey');//生成二维码生成连接的参数
        $code=config('QrCode');//加密的二维码参数
        $time=time();
        $checkSum=sha1($appkey.$code.$time);
       
       // dump($checkSum);
        //$user_id=4;//用户的ID
        $date=date('Ymd');
        $user_id=input('post.uid');
       
        $place_id=empty(input('post.place_id'))?ajaxReturn2(-1,'place_id is miss',''):input('post.place_id');
        //$fee=input('get.fee');//场地生成二维码的价格
        //$xishu=input('get.xishu');//场地生成二维码的系数
        //$startime=input('get.startime');//验证参数修改的开始时间
        //$endtime=input('get.endtime');//验证参数的场地预约的结束时间
        $datetime=input('post.riqi');
        $appointime=input('post.appoint_time');
        $para=array('user_id'=>$user_id,'place_id'=>$place_id,'checkSum'=>$checkSum,'time'=>$time,'riqi'=>$datetime,'appint_time'=>$appointime);
        $para=http_build_query($para);
        $text="http://www.ehuangpu.org?".$para;
        
        $aimUrl="uploads/qrcodes/$user_id/$date";
        $FileUtil=new FileUtil();
        if(!is_dir($aimUrl)){
            $FileUtil->createDir($aimUrl);
        }
      self::$Qrcode->png($text,$user_id.'.png');
      //生成新的二维码的图片 
      
      $fileUrl=ROOT_PATH.'public'.DS.$user_id.'.png';
      $qr_code_address=$aimUrl.'/'.$user_id.'.png';
      $aimUrl=ROOT_PATH.'public'.DS.$aimUrl.'/'.$user_id.'.png';
     
     $res=$FileUtil->moveFile($fileUrl, $aimUrl);
     if($res){
         //这里操作数据库
         $data=array('uid'=>$user_id,'place_id'=>$place_id,'qr_code_address'=>$qr_code_address,'create_time'=>$time,'appoint_time'=>$appointime,'riqi'=>$datetime);
         $result=self::$QrCodeModel->add($data);
         
         $r=array('url'=>url('api/Qrcode/index2'),'address'=>$qr_code_address,'time'=>$time,'id'=>$result);
         if($result){
            $order_id=Db::name('appointment_gym')->where(array('user_id'=>$user_id,'place_id'=>$place_id,'appoint_time'=>$appointime,'riqi'=>$datetime))->field('order_id')
             ->find()['order_id'];
            Db::name('order')->where(array('id'=>$order_id))
            ->update(array('status'=>2));
             ajaxReturn2(1,'insert sucess',json_encode($r));
         }else{
             ajaxReturn2(0,'insert failed',$data);
         }
     }else{
         ajaxReturn2(0,'move failed','');
     }
      
        
    }
    public function test(){
        $fileUtil=new FileUtil();
       // $fileUtil->createDir('a/12/3');
        $fileUtil->createFile('a/12/3/3.txt',true);
    }
    //这里进行处理商家扫码的user_id 查询出用户的信息 查询出用的订单号 status=1表明是
    //明天根据具体的内容进行商家端用户端进行处理 多表查询 (+订单的处理）
    public function ProcessCode(){
       
       $checkSum=empty(input('get.checkSum'))?ajaxReturn2(-1,'your action is invalidate','sb is it'):input('get.checkSum');
       
       
       $user_id=empty(input('get.user_id'))?ajaxReturn2(-1,'user_id is miss',''):input('get.user_id');
       $place_id=empty(input('get.place_id'))?ajaxReturn2(-1,'place_id is miss',''):input('get.place_id');
       $res= $this->checkValidate($user_id, $place_id, $checkSum);
       if(!$res){
           ajaxReturn2(0,'your request is invaldate','fuck your mother');
       }
       $appoint_time=empty(input('get.appoint_time'))?ajaxReturn2(-1,'app is miss'):input('get.appoint_time');
       $riqi=empty(input('get.riqi'))?ajaxReturn2(-1,'riqi is miss',''):input('get.riqi');
       //查询数据库整理数据显示
//        $where=array('uid'=>$user_id,'appoint_time'=>$appoint_time,'place_id'=>$place_id,'riqi'=>$riqi);
//        $res=self::$QrCodeModel->selectQrCode($where);
       $nickname=self::$QrCodeModel->getUserNickName($user_id);
       $time=time();
       $endtime=$time;
      
       $startime=input('get.time');//开始健身的时间
      
        
       $veModel=new Venue();
       $veppointModel=new Venueappoint();
       
       $jis=$veModel->getFee(array('place_id'=>$place_id));
    
       $pay=(($endtime-$startime)/3600)*$jis['fee']*$jis['xishu'];
       $pay=round($pay);
       $info=$veModel->getItem($place_id);
       
       $condtion=array('user_id'=>$user_id,'riqi'=>$riqi,'place_id'=>$place_id,'appoint_time'=>$appoint_time);
        
       $c_info=$veppointModel->getappointInfo($condtion);
      //我们这里默认给当扫描这个二维码的时候 生成这些数据信息之前就改变这个状态
      //修改二维码的状态和结束时间
      $where=array('uid'=>$user_id,'place_id'=>$place_id,'appoint_time'=>$appoint_time,'riqi'=>$riqi);
      self::$QrCodeModel->updatestatus($where, $endtime);
      
      //------------修改订单状态
      $order_id=Db::name('appointment_gym')->where(array('user_id'=>$user_id,'place_id'=>$place_id,'appoint_time'=>$appoint_time,'riqi'=>$riqi))->field('order_id')
      ->find()['order_id'];
      Db::name('order')->where(array('id'=>$order_id))
      ->update(array('status'=>4));
      
       $count=array_to_fe($appoint_time);
       $data_new=[];
       $data_new['user_info']=$nickname;
       $data_new['pay_info']=$pay;
       $data_new['appoint_time']=$appoint_time;
       $data_new['riqi']=$riqi;
       $data_new['detail']=$info;
       $data_new['count']=$count;
       $data_new['c_info']=$c_info;
       //dump($data_new);
       return view('jishen/scan',array('data'=>$data_new));
      
      //验证参数是否改过
//       $where=array('place_id'=>$place_id,'startime'=>$startime,'endtime'=>$endtime);
//       $Validate=self::$QrCodeModel->getFeeAndX($where);
//       if(!$Validate){
//           ajaxReturn2(0,'feifaxiugaicanshu','you is sb');
//       }
//       $fee=$Validate['fee'];
//       $xishu=$Validate['xishu'];
      //下面根据user_id 查询用户的订单信息 并且需要支付的金额 $fee*$xishu 
      //
    }
    public function checkValidate($user_id,$place_id,$checkSum){
         $appkey=config('QrKey');//生成二维码生成连接的参数
         $code=config('QrCode');//加密的二维码参数
         $res= Db::name('qr_code')->field('create_time')->where(array('uid'=>$user_id,'place_id'=>$place_id))->order('create_time desc')->find();
        if(!empty($res)){
           $currentime=$res['create_time'];
           $Validate=sha1($appkey.$code.$currentime);
           if($checkSum==$Validate){
               return 1;
           }else{
               return 0;
           }
        }
       
    }
    public function ParameValidate($where){
        self::$QrCodeModel->getFeeAndX($where);
    }
    public function test2(){
        $time='1511233697';
        echo date('Y-m-d H:i:s',$time);
        $current=time();
       echo  round(($current-$time)/3600,2);
       
        
    }
    public function ce(){
        $VMdel=new Venueappoint();
        $page_num=input('get.page_num');
        $page_size=input('get.page_size');
        $VMdel->MyselectInfos(3,'2017.11.9');
    }
    /**
     * 点击开始健身跳转到健身页面
     */
    public function index(){
        
        $where=input('get.');
        //session('user_id',1);
        $user_id=session('user_id');
        $where['uid']=$user_id;
        $res=self::$QrCodeModel->selectQrCode($where);
       //dump($where);
        if(empty($res)){
            return view('jishen/start_1',array('para'=>json_encode($where)));
        }else{
            if($res['status']==0){
              
               // header('Location: http://www.example.com/');
               return redirect(url('api/Qrcode/index3'),array('id'=>$res['id'],'time'=>$res['create_time']));
            }else{
                //查询数据库整理数据显示
                $nickname=self::$QrCodeModel->getUserNickName($user_id);
                $endtime=$res['endtime'];//结束时间
               // echo $endtime;
               //echo '<br/>';
                $startime=$res['create_time'];//开始健身的时间
                //echo $startime;
                $appoint_time=$res['appoint_time'];
             
                $veModel=new Venue();
                $veppointModel=new Venueappoint();
                //echo $res['place_id'];
                $jis=$veModel->getFee(array('place_id'=>$res['place_id']));
                //dump($jis);
                $pay=(($endtime-$startime)/3600)*$jis['fee']*$jis['xishu'];
                $pay=round($pay);
                $info=$veModel->getItem($res['place_id']);
                
                $condtion=array('user_id'=>$user_id,'riqi'=>$res['riqi'],'place_id'=>$res['place_id'],'appoint_time'=>$res['appoint_time']);
               
                $c_info=$veppointModel->getappointInfo($condtion);
                //dump($c_info);
                $count=array_to_fe($res['appoint_time']);
                $data_new=[];
                $data_new['user_info']=$nickname;
                $data_new['pay_info']=$pay;
                $data_new['appoint_time']=$appoint_time;
                $data_new['riqi']=$res['riqi'];
                $data_new['detail']=$info;
                $data_new['count']=$count;
                $data_new['c_info']=$c_info;
                //dump($data_new);
                return view('jishen/scan',array('data'=>$data_new));
                //这里进行获取信息 进行支付页面 订单
            }
        }
       
    }
    public function index2(){
        
        $user_id=session('user_id');
        $id=input('get.id');
        $res=self::$QrCodeModel->selectQrcodeById($id);
        $status=$res['status'];
        if($status==0){
        $address=input('get.address');
        $time=input('get.time');
        $end=time()-$time;
        $tim=time_line2($end);
        //$currentime=time();
       //dump($tim);
        //echo $currentime-$time;
        //dump($edtime);
        return view('jishen/start_2',array('pa'=>array('address'=>$address,'time'=>$tim)));
        } else{
                $nickname=self::$QrCodeModel->getUserNickName($user_id);
                $endtime=$res['endtime'];//结束时间
                // echo $endtime;
                //echo '<br/>';
                $startime=$res['create_time'];//开始健身的时间
                //echo $startime;
                $appoint_time=$res['appoint_time'];
            
                $veModel=new Venue();
                $veppointModel=new Venueappoint();
                //echo $res['place_id'];
                $jis=$veModel->getFee(array('place_id'=>$res['place_id']));
                //dump($jis);
                $pay=(($endtime-$startime)/3600)*$jis['fee']*$jis['xishu'];
                $pay=round($pay);
                $info=$veModel->getItem($res['place_id']);
                 
                $condtion=array('user_id'=>$user_id,'riqi'=>$res['riqi'],'place_id'=>$res['place_id'],'appoint_time'=>$res['appoint_time']);
            
                $c_info=$veppointModel->getappointInfo($condtion);
                //dump($c_info);
                $count=array_to_fe($res['appoint_time']);
                $data_new=[];
                $data_new['user_info']=$nickname;
                $data_new['pay_info']=$pay;
                $data_new['appoint_time']=$appoint_time;
                $data_new['riqi']=$res['riqi'];
                $data_new['detail']=$info;
                $data_new['count']=$count;
                $data_new['c_info']=$c_info;
                //dump($data_new);
                return view('jishen/scan',array('data'=>$data_new));
                //这里进行获取信息 进行支付页面 订单
            }
        
    }
    public function index3($id,$time){
//         $address=input('get.address');
//         $time=input('get.time');
//这里进行
        //session('user_id',6);
        $user_id=session('user_id');
        $res=self::$QrCodeModel->selectQrcodeById($id);
        $status=$res['status'];
        if($status==0){
        $address=self::$QrCodeModel->selectItemAdress($id)['qr_code_address'];
        $end=time()-$time;
        $tim=time_line2($end);
        //$currentime=time();
        //dump($tim);
        //echo $currentime-$time;
        //dump($edtime);
        return view('jishen/start_2',array('pa'=>array('address'=>$address,'time'=>$tim)));
         }else{
             $nickname=self::$QrCodeModel->getUserNickName($user_id);
             $endtime=$res['endtime'];//结束时间
             // echo $endtime;
             //echo '<br/>';
             $startime=$res['create_time'];//开始健身的时间
             //echo $startime;
             $appoint_time=$res['appoint_time'];
              
             $veModel=new Venue();
             $veppointModel=new Venueappoint();
             //echo $res['place_id'];
             $jis=$veModel->getFee(array('place_id'=>$res['place_id']));
             //dump($jis);
             $pay=(($endtime-$startime)/3600)*$jis['fee']*$jis['xishu'];
             $pay=round($pay);
             $info=$veModel->getItem($res['place_id']);
             
             $condtion=array('user_id'=>$user_id,'riqi'=>$res['riqi'],'place_id'=>$res['place_id'],'appoint_time'=>$res['appoint_time']);
              
             $c_info=$veppointModel->getappointInfo($condtion);
             //dump($c_info);
             $count=array_to_fe($res['appoint_time']);
             $data_new=[];
             $data_new['user_info']=$nickname;
             $data_new['pay_info']=$pay;
             $data_new['appoint_time']=$appoint_time;
             $data_new['riqi']=$res['riqi'];
             $data_new['detail']=$info;
             $data_new['count']=$count;
             $data_new['c_info']=$c_info;
             //dump($data_new);
             return view('jishen/scan',array('data'=>$data_new));
             //这里进行获取信息 进行支付页面 订单
         }
    }
    public function test3(){
        echo time()+3600;
    }
    //状态健身结束的状态
    public function evatie(){
        
    }
    public function ec(){
        $appkey=config('QrKey');//生成二维码生成连接的参数
        $code=config('QrCode');//加密的二维码参数
       echo time()+3600*0.76;
       
        
    }
}